﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Cms.Web.Framework.Extensions
{
    public static class PageExtensions
    {
        public static void AddHeaderScriptInclude(this Page page, string src)
        {
            IList<string> registeredScripts;
            if (HttpContext.Current.Items["RegisteredHeaderScriptIncludes"] != null)
                registeredScripts = (IList<string>)HttpContext.Current.Items["RegisteredHeaderScriptIncludes"];
            else
                registeredScripts = new List<string>();
            if (registeredScripts.Contains(src))
                return;
            registeredScripts.Add(src);
            HttpContext.Current.Items["RegisteredHeaderScriptIncludes"] = registeredScripts;
            var js = new HtmlGenericControl("script");
            js.Attributes.Add("type", "text/javascript");
            js.Attributes.Add("src", src);
            page.Header.Controls.Add(js);
        }
        
        public static void AddHeaderScript(this Page page, string key, string src)
        {
            IDictionary<string, string> registeredScripts;
            if (HttpContext.Current.Items["RegisteredHeaderScripts"] != null)
                registeredScripts = (IDictionary<string, string>)HttpContext.Current.Items["RegisteredHeaderScripts"];
            else
                registeredScripts = new Dictionary<string, string>();
            if (registeredScripts.ContainsKey(key))
                return;
            registeredScripts.Add(key, src);
            HttpContext.Current.Items["RegisteredHeaderScripts"] = registeredScripts;
            var js = new HtmlGenericControl("script");
            js.Attributes.Add("type", "text/javascript");
            js.InnerText = src;
            page.Header.Controls.Add(js);
        }

        public static void AddHeaderStyleLink(this Page page, string src)
        {
            IList<string> registeredLinks;
            if (HttpContext.Current.Items["RegisteredHeaderStyleLinkIncludes"] != null)
                registeredLinks = (IList<string>)HttpContext.Current.Items["RegisteredHeaderStyleLinkIncludes"];
            else
                registeredLinks = new List<string>();
            if (registeredLinks.Contains(src))
                return;
            registeredLinks.Add(src);
            HttpContext.Current.Items["RegisteredHeaderStyleLinkIncludes"] = registeredLinks;
            var link = new HtmlGenericControl("link");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("href", src);
            page.Header.Controls.Add(link);
        }

        public static Cms.Web.Framework.Controls.Page CmsPage(this Page page)
        {
            if (page is Controls.Page)
            {
                return (Controls.Page)page;
            }
            throw new Exception("Page is not of type Cms.Web.Framework.Controls.Page");
        }
    }
}